package com.ghpt.controller;

import com.ghpt.pojo.Result;
import com.ghpt.pojo.UpdatableEntity;
import com.ghpt.service.impl.BatchUpdateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/import")
public class BatchUpdateController {

    @Autowired
    private BatchUpdateService batchUpdateService;

    @PostMapping("/csv/{entityType}")
    public Result<String> updateDataFromCsv(
            @PathVariable String entityType,
            @RequestParam("file") MultipartFile file) {
        try {
            Class<?> clazz = Class.forName("com.ghpt.pojo." + entityType);
            if (!UpdatableEntity.class.isAssignableFrom(clazz)) {
                return Result.error("不支持的实体类型");
            }

            batchUpdateService.batchUpdate(file, (Class<? extends UpdatableEntity>) clazz);
            return Result.success("数据加载成功");  // 现在可以正常使用
        } catch (IllegalArgumentException e) {
            return Result.error("文件格式错误：" + e.getMessage());
        } catch (Exception e) {
            return Result.error("更新失败：" + e.getMessage());
        }
    }
}